package com.edu.admin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysSchoolRole;
import com.edu.admin.entity.SysSchoolRoleMenu;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.enums.UseTypeEnum;
import com.edu.admin.exception.SysException;
import com.edu.admin.mapper.SchoolRoleMapper;
import com.edu.admin.mapper.SchoolRoleMenuMapper;
import com.edu.admin.vo.SchoolRoleMenuVo;
import com.edu.admin.vo.SchoolRoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 校级角色服务
 * 
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class SchoolRoleService extends BaseService<SchoolRoleMapper, SysSchoolRole> {
	@Autowired
	SchoolRoleMenuMapper roleMenuMapper;
	
	/**
	 * 获取校级角色列表
	 * 
	 * @param schoolId
	 * 
	 * @return List<SchoolRoleVo>
	 */
	public List<SchoolRoleVo> getSchoolRoleListBySchoolId(Long schoolId) {
		List<SchoolRoleVo> roleList = this.mapper.getSchoolRoleList(schoolId,UseTypeEnum.ISUSE.getCode());
		for (SchoolRoleVo roleVo : roleList)
		{
			List<SchoolRoleMenuVo> roleMenuList = roleMenuMapper.getSchoolRoleMenuListBySchoolRoleId(roleVo.getSchoolRoleId());
			Long[] menuIds = new Long[roleMenuList.size()];
			int i =0 ;
			for (SchoolRoleMenuVo roleMenuVo : roleMenuList)
			{
				menuIds[i] = roleMenuVo.getMenuId();
				i++;
			}
			roleVo.setMenuIds(menuIds);
		}
		return roleList;
	}
	
	/**
	 * 获取校级角色列表 （分页）
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param schoolId
	 * 
	 * @return PageInfo<SchoolRoleVo>
	 */
	public PageInfo<SchoolRoleVo> getAllSchoolRoleListPageBySchoolId(Integer pageNum, Integer pageSize, Long schoolId) {
		
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<SchoolRoleVo> roleList = this.mapper.getSchoolRoleList(schoolId,null);
		for (SchoolRoleVo roleVo : roleList)
		{
			List<SchoolRoleMenuVo> roleMenuList = roleMenuMapper.getSchoolRoleMenuListBySchoolRoleId(roleVo.getSchoolRoleId());
			Long[] menuIds = new Long[roleMenuList.size()];
			int i =0 ;
			for (SchoolRoleMenuVo roleMenuVo : roleMenuList)
			{
				menuIds[i] = roleMenuVo.getMenuId();
				i++;
			}
			roleVo.setMenuIds(menuIds);
		}
		
		PageInfo<SchoolRoleVo> pageInfo = new PageInfo<SchoolRoleVo>(roleList);

		return pageInfo;
	}
	
	/**
     * 校级角色重复性检查
     * 
     * @param SysSchoolRole role
     * 
     * @return List<SchoolRoleVo>
     * @throws Exception
     */
    public List<SchoolRoleVo> checkSchoolRole(SysSchoolRole role) throws Exception {
    	
    	return this.mapper.findSchoolRoleBySchoolRoleName(role.getSchoolId(), role.getSchoolRoleId(), role.getSchoolRoleName().trim());
  
    }
	
	/**
	 * 校级角色保存
	 * 
	 * @param role
	 * 
	 * @return Map<String, Object>
	 * @throws Exception 
	 */
	public Map<String, Object> saveSchoolRole(SysSchoolRole role) throws Exception {
		Map<String, Object> result = new HashMap<>();
		
		List<SchoolRoleVo> roleList = checkSchoolRole(role);
		
		if (roleList!=null && roleList.size()>0)
		{
			throw new SysException(ExceptionEnum.SCHOOLROLENAMEREPEAT);
		}
		
		Long roleId = role.getSchoolRoleId();

		if (roleId == null) {
			
			role.setUseType(0);
			
			insert(role);
			
			roleId = role.getSchoolRoleId();

		} else {

			updateByPrimaryKeySelective(role);

		}
		
		result.put("roleId", roleId);

		return result;

	}
	
	/**
	 * 删除校级角色
	 * 
	 * @param schoolRoleId
	 * 
	 * @return int
	 */
	public int deleteSchoolRole(Long schoolRoleId) {
		
		if (schoolRoleId != null) {
			return this.deleteByPrimaryKey(schoolRoleId);

		}
		
		return 0;	

	}
	
	/**
	 * 启用停用校级角色
	 * 
	 * @param roleId
	 * @param useType
	 * 
	 * @return int
	 */
	public int useSchoolRole(Long schoolRoleId,Integer useType) {
		
		if (schoolRoleId != null) {
			SysSchoolRole role = new SysSchoolRole();
			role.setSchoolRoleId(schoolRoleId);
	        role.setUseType(useType);
			return updateByPrimaryKeySelective(role);

		}
		
		return 0;	

	}
	
	/**
     * 新增校级角色菜单信息
     * 
     * @param role
     * 
     * @return
     */
    public void insertSchoolRoleMenu(SysSchoolRole role)
    {

        List<SysSchoolRoleMenu> list = new ArrayList<SysSchoolRoleMenu>();
        for (Long menuId : role.getMenuIds())
        {
            SysSchoolRoleMenu rm = new SysSchoolRoleMenu();
            rm.setSchoolRoleId(role.getSchoolRoleId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            roleMenuMapper.batchSave(list);
        }

    }



}
